<template>
  <!-- 对话框 -->
  <transition name="dialog-fade">
    <div class="lm-dialog_wrapper" v-show="visible" @click.self="closeVisible">
      <div class="lm-dialog" :style="{ width, marginTop }">
        <div class="lm-dialog_header">
          <slot name="title">
            <span class="lm-dialog_title">{{ title }}</span>
          </slot>
          <button class="lm-dialog_headerbtn" @click="closeVisible">
            <i class="lm-icon-close"></i>
          </button>
        </div>
        <div class="lm-dialog_body">
          <slot>
            <span>这是一段信息</span>
          </slot>
        </div>
        <div class="lm-dialog_footer" v-if="$slots.footer">
          <slot name="footer">
            <lm-button>取消</lm-button>
            <lm-button type="primary">确定</lm-button>
          </slot>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
export default {
  name: "LmDialog",
  props: {
    title: {
      type: String,
      default: "提示",
    },
    width: {
      type: String,
      default: "50%",
    },
    marginTop: {
      type: String,
      default: "15vh",
    },
    visible: {
      type: Boolean,
      default: false,
    },
  },
  watch: {
    visible(newVal) {
      if (newVal) {
        this.$emit("opened");
      } else {
        this.$emit("closed");
      }
    },
  },
  methods: {
    //   关闭弹窗
    closeVisible() {
      this.$emit("update:visible", false);
    },
  },
};
</script>

<style lang="scss" scope>
// scope会给当前组件的模板中的所有元素都添加一个随机的属性
// scope会给当前组件中所有的样式页添加一个对应的属性选择器
.lm-dialog_wrapper {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  overflow: auto;
  margin: 0;
  z-index: 2001;
  background-color: rgba(0, 0, 0, 0.5);
  .lm-dialog {
    position: relative;
    margin: 15vh auto 50px;
    background: #fff;
    border-radius: 2px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
    box-sizing: border-box;
    width: 30%;
    &_header {
      padding: 20px 20px 10px;
      .lm-dialog_title {
        line-height: 24px;
        font-size: 18px;
        color: #303133;
      }
      .lm-dialog_headerbtn {
        position: absolute;
        top: 20px;
        right: 20px;
        padding: 0;
        background: transparent;
        border: none;
        outline: none;
        cursor: pointer;
        font-size: 16px;
        .lm-icon-close {
          color: 909399;
        }
      }
    }
    &_body {
      padding: 30px 20px;
      color: #606266;
      font-size: 14px;
      word-break: break-all;
    }
    &_footer {
      padding: 10px 20px 20px;
      text-align: right;
      box-sizing: border-box;
      //   深度选择器scss中是 ::v-deep less中是 /deep css中是 >>>
      // 加了深度选择器就不会像scope那样添加属性选择器
      ::v-deep .lm-button:first-child {
        margin-right: 20px;
      }
    }
  }
}
// 进入动画
.dialog-fade-enter-active {
  animation: fade 0.3s;
}
// 离开的动画
.dialog-fade-leave-active {
  animation: fade 0.3s reverse;
}
@keyframes fade {
  0% {
    opacity: 0;
    transform: translateY(-20px);
  }
  100% {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>